#include <iostream>
#include <list>
#include <string>
using namespace std;
std::ostream &operator<<(std::ostream &ostr, std::list<int> &list)
{
    for (std::list<int>::iterator it = list.begin(); it != list.end(); it++)
    {
        ostr << " " << *it;
    }
    return ostr;
}
bool comp(double x, double y)
{
    return x > y;
}

int main()
{
    list<int> l;
    l.push_back(1);
    l.push_back(2);
    l.push_back(3);
    l.push_back(4);
    l.push_front(5);

    // traversal
    for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    cout << "list's size:" << l.size() << endl;
    cout << "list asc sort:";
    l.sort();
    for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;
    cout << "list des sort:";
    l.sort(comp);
    for (list<int>::iterator it = l.begin(); it != l.end(); it++)
    {
        cout << *it << " ";
    }
    cout << endl;

    // merge
    int myints[] = {5, 9, 0, 1, 3};
    int myints2[] = {8, 7, 2, 6, 4};
    list<int> list1(myints, myints + 5);
    list<int> list2(myints2, myints2 + 5);

    list1.sort();
    list2.sort();
    cout << "list1:  " << list1 << "\n";
    cout << "list2:  " << list2 << "\n";
    list1.merge(list2);
    cout << "merged: " << list1 << "\n";

    // pop
    list1.pop_back();
    list1.pop_front();
    cout << "after pop_back() and pop_front(): " << list1 << "\n";

    return 0;
}
